<html>
<head>
	<title>unittest for html_extractor</title>
	<script src="jsunittest/jsunittest.js" type="text/javascript"></script>
	<script src="../html_extractor-0.0.1.js" type="text/javascript"></script>
	<link rel="stylesheet" href="jsunittest/unittest.css" type="text/css" /> 
</head>
<body>
	<div id="testlog"></div>
</body>
</html>

<script type="text/javascript">
  new Test.Unit.Runner({
    setup: function() {
    },
    
    teardown: function() {
    },
    testMatchScript: function() { with(this) {
		var html = '<head><script>alert("hello");<\/script></head>';
		var ret = html.jhe().tag("head").im();
		assertEqual('alert("hello");', ret);
	}},	
	testNextTag2: function() { with(this) {
				var html  = "<div><span>11</span><span>12</span></div><div><span>11</span><span>12</span></div>";
				var ret = html.jhe().tag("div").next_tag("span").im();
				assertEqual(2, ret.length);
				var ret = html.jhe().tag("div").tag("span").im();
				assertEqual(4, ret.length);
				
				var html  = "<ol><!--mm--><li class=g><h2>11</h2></li><li  class=g>12</li></ol>";
				var ret = html.jhe().tag("ol").next_tag("li").attr("class", "g").im();
				assertEqual(1, ret.length);
				assertEqual('<h2>11</h2>', ret[0]);
		}},
		testBrockenHtml: function() { with(this) {
				var html = "<div><p>hello<p id='hello'><input id='input'><p>aa</div>";
				var jhe = html.jhe();
				var ret = jhe.tag("div").tag("p").im();
				assertEqual(3, ret.length);
				assertEqual("hello", ret[0]);
				assertEqual('<input id="input">', ret[1]);		
		}},
	testEmptyTag: function() { with(this) {
		    var html = "<div><input id='hello'></div>";
			var ret = new html_extractor(html).tag("div").tag("input").ma("id");
			assertEqual("hello", ret);
		}},
 	testMatchTag: function() { with(this) {
			var html = '<div><p>text</p></div>';
			var ret = new html_extractor(html).match();
			assertEqual(1, ret.length, "test1");			
			assertEqual("<p>text</p>", ret[0], "test2");
		}},
	testMatchByAttr: function() { with(this) {
			var html = '<div class="t1"><a href="http://www.sina.com.cn">sina</a></div><div id=11 class="t1"><a href="http://www.google.cn">google</a></div>';
			var ret = new html_extractor(html).attr("class", "t1").tag("a").match();			
			assertEqual(2, ret.length);
			assertEqual("sina", ret[0]);
			assertEqual("google", ret[1]);
			
			var ret = new html_extractor(html).tag("*").attr("class", "t1").tag("a").match();			
			assertEqual(2, ret.length);
			assertEqual("sina", ret[0]);
			assertEqual("google", ret[1]);
			
			var ret = new html_extractor(html).tag("*").attr("class", "t1").attr("id", "11").tag("a").match();			
			assertEqual(1, ret.length);
			assertEqual("google", ret[0]);			
						
			
		}},
	testMatchFirstTag: function() { with(this) {		
				var html = '<p><div><div><a href="http://www.sina.com.cn">text</a></div></div></p>';
				var ret = new html_extractor(html).first_tag("p").match();
				assertEqual(1, ret.length, "test1");			
				var ret = new html_extractor(html).first_tag("div").match();
				assertEqual(0, ret.length, "test2");							

			}},
	testMatchAttr: function() { with(this) {
			var html = '<a href="http://www.sina.com.cn">text</a>';
			var ret = new html_extractor(html).ma("href");
			assertEqual(1, ret.length);			
			assertEqual("http://www.sina.com.cn", ret[0]);
			
		    html = '<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head>';
		    ret = new html_extractor(html).tag("meta").ma("http-equiv");
			assertEqual(1, ret.length);
			assertEqual("content-type", ret[0]);		
			
		}},
		
	testMatchText: function() { with(this) {
			var html = '<div><p>text</p></div>';
			var ret = new html_extractor(html).match_text();
			assertEqual(1, ret.length);			
			assertEqual("text", ret[0]);
		}},
		
    testChildMatch: function(){ with(this) {
		var html = "<div><div><div>item1</div></div></div>" +
		           "<div><p><div><div>item2</div></div></p></div>" + 
		           "<div><p><div><p><div>item3</div></p></div></p></div>" +
		           "<div><p><div><p><center><div>item4</div></center></p></div></p></div>";
		var extractor = new html_extractor(html);
		var ret  = extractor.tag("div").next_tag("div").next_tag("div").match();
		assertEqual(1, ret.length);
		assertEqual("item1", ret[0]);		

		ret  = extractor.tag("div").next_tag("div").match();
		assertEqual(2, ret.length);
		assertEqual("<div>item1</div>", ret[0]);				
		assertEqual("item2", ret[1]);						

		ret  = extractor.tag("div").next_tag("p").next_tag("div").next_tag("p").next_tag("div").match();
		assertEqual(1, ret.length);
		assertEqual("item3", ret[0]);			

		ret  = extractor.tag("div").next_tag("p").next_tag("div").next_tag("p").tag("div").match();
		assertEqual(2, ret.length);
		assertEqual("item3", ret[0]);					
		assertEqual("item4", ret[1]);	

		ret  = extractor.tag("div").tag("p").next_tag("center").match();
		assertEqual(1, ret.length);
		assertEqual("<div>item4</div>", ret[0]);															
	}},
		
    testMatch: function() { with(this) {
		var html = "<div><div><div id='aa'><a href='bb'>bb</a>aa</div>tt</div>tt</div>";
		ret = new html_extractor(html).tag("div").tag("div").tag("div").attr("id", "aa").match();
		assertEqual("<a href=\"bb\">bb</a>aa", ret[0]);
		ret = new html_extractor(html).tag("div").tag("div").tag("div").attr("id", "aa").match(false);		
		assertEqual("<div id=\"aa\"><a href=\"bb\">bb</a>aa</div>", ret[0]);

		html = '<div>' + 
			'<div id="div1"><span>list1</span><ul><li>item111</li><li>item12</li></ul></div>' +
			'<div id="div2"><span>list2</span><ul><li>item121</li><li><a href="http://www.sina.com.cn">item22</a></li><li>item123</li><li>item24</li></ul></div>' +
			'</div>';
			
		
		ret = html.jhe().tag("div").tag("div").attr("id", /div\d*/).im(function(idx, attrs, content)
			{
				var result = {};
				var extractor = new html_extractor(content);
				result.title = extractor.tag("span").match();
				result.items = extractor.tag("li").match();				
				return 		result;
			}
		);		
		assertEqual(2, ret.length);		
		assertEqual(2, ret[0].items.length);		
		assertEqual(4, ret[1].items.length);				
		assertEqual("list1", ret[0].title);		
		assertEqual("list2", ret[1].title);		

		assertEqual("item22", ret[1].items[1].jhe().im());	
		assertEqual("http://www.sina.com.cn", ret[1].items[1].jhe().ma("href"));	
					
		 

		var extractor = new html_extractor("<a href=\"http://www.sina.com.cn\">sina</a>")
		ret = extractor.tag("a").match_attr("href");
		assertEqual("http://www.sina.com.cn", ret[0]);				
		ret = extractor.tag("a").match();
		assertEqual("sina", ret[0]);		
		ret = extractor.tag("a").match(false);
		assertEqual('<a href="http://www.sina.com.cn">sina</a>', ret[0]);				

		var ret = new html_extractor("<div><div><div>aaa</div></div>").tag("div").match();
		assertEqual("<div><div>aaa</div></div>", ret);

    }},

   testIm: function() { with(this) {
		var html = "<div><p>abbc</div>";
		assertEqual("abbc", html.jhe().tag("p").im());
   	    }},
   testOm: function() { with(this) {
		var html = "<div><p>abbc</div><div id=11><p>11</div>";
		assertEqual("<p>11</p>", html.jhe().tag("div").attr("id", "11").tag("p").om());		
   	    }},		
   testMa: function() { with(this) {
		var html = "<div><p id='abbc'>abbc</div><div><p id='11'>11</div>";
		assertEqual(2, html.jhe().tag("p").ma('id').length);
   	    }},
   testMt: function() { with(this) {
		var html = "<div><p id='abbc'>abbc</div><div><p id='11'>11</div>";
		assertEqual('11', html.jhe().tag("p").attr('id', '11').mt());				
   	    }},			
	testExtractQuery: function() { with(this) {
				var html = "<body><!--m--><div class='11'>11</div><div class='12'>12</div></body>";
				var match = function match(params){
						return html_extractor_query(html, arguments).im();											
				};
				var ret = match('body', 'div');
				assertEqual(2, ret.length);
				ret = match('body', '>div');
				assertEqual(11, ret[0]);								
				ret = match('div', '@class=12');
				assertEqual(12, ret[0]);
				ret = match("*", 'div', '@class=12');
				assertEqual(12, ret[0]);
				ret = match("^body", 'div', '@class=12');
				assertEqual(12, ret[0]);
				ret = match('^div', '@class=12');
				assertEqual(0, ret.length);
				ret = match('div', '@@class=[12]{2}');
				assertEqual(2, ret.length);
		}},		
	testJheIm: function() { with(this) {
		var html = "<div><p>abbc</div><div id=11><p><b>11</b></div>";
		assertEqual("<b>11</b>", html.jhe_im("div", "@id=11", "p"));
		var ret = html.jhe_im("div", "@id=11", "p", function(idx, attrs, content){
				return "<i>" + content + "</i>";
				});
		assertEqual('<i><b>11</b></i>', ret);
		}},
	testJheOm: function() { with(this) {
		var html = "<div><p>abbc</div><div id=11><p>11</div>";
		assertEqual("<p>11</p>", html.jhe_om("div", "@id=11", "p"));		
		}},
	testJheMa: function() { with(this) {
		var html = "<div><p id='abbc'>abbc</div><div><p id='11'>11</div>";
		assertEqual(2, html.jhe_ma("div", "p", "id").length);
		html = '<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head>';		
		ret = html.jhe_ma("meta", "http-equiv");
		assertEqual("content-type", ret);
		}},
	testJheMt: function() { with(this) {
				var html = "<div><p id='abbc'>abbc</div><div><p id='11'>11</div>";
				assertEqual('abbc', html.jhe_mt("div", ">p", "@id=abbc"));						
		}},				
  }); 
</script>